<?php

namespace App\Jobs;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldBeUnique;

class RewardCountJob implements ShouldQueue
{
	use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

	public User $user;

	public function __construct(User $user)
	{
		$this->user = $user;
	}

	public function handle()
	{
        \Log::info("count prize");

        $user = $this->user;
        $directPrize = $user->todayDirectPrize();
        $teamPrize = $user->todayTeamPrize();

        if ($directPrize > 0) {
            \Log::info("count prize direct > 0");
            account_coins($user)->plusGoldRecordDirectPrize($directPrize);
            if ($user->gold_subsidies > 0
                and $directPrize >= $user->gold_subsidies) {
                account_coins($user)->plusUnlockedSubsidise();
            }
        }

        if ($teamPrize > 0) {
            \Log::info("count prize team > 0");
            account_coins($user)->plusGoldRecordTeamPrize($teamPrize);
        }
	}
}
